//
//  CXPaymentLoadingView.swift
//  Asman
//
//  Created by 李旭峰 on 6/17/19.
//  Copyright © 2019 李旭峰. All rights reserved.
//

import UIKit

class CXPaymentLoadingView: UIView {
  
    @IBOutlet weak var loadingContiner: UIView!
    @IBOutlet weak var lbContent: CXLabel!
    @IBOutlet weak var loadingImage: UIImageView!
  
    fileprivate var isLoading:Bool = false
    
    let contentView:UIView = UIView()
    let contentText:CXLabel = CXLabel()
    let loadingImageView:UIImageView = UIImageView()
    let cq_loadingView:UIActivityIndicatorView = UIActivityIndicatorView(style: .gray)
    let btnCancel:CXPowerButton = CXPowerButton(type: .text)
  
    var cancelAction:(()->())?
  
    func config(){
      self.contentView.layer.masksToBounds = true
      self.contentView.layer.cornerRadius = 5.0
      self.contentView.backgroundColor = UIColor(hexStr: "#000000", alpha: 0.4)

      self.cq_loadingView.isUserInteractionEnabled = false
      self.cq_loadingView.color = UIColor.gray
      
      self.contentText.cxFont = FontAsset.PingFangSC_Regular.size(12.0)
      self.contentText.cxTextColor = UIColor(hexStr: "#FFFFFF", alpha: 1.0)
      
      self.btnCancel.paddingValue = UIEdgeInsets(top: 15.0, left: 15.0, bottom: 15.0, right: 15.0)
      self.btnCancel.textLabel.openShadow = true
      self.btnCancel.setText("\u{e705}", for: .normal)
      self.btnCancel.setTextColor(UIColor.white, for: .normal)
        // TODO:>
        self.btnCancel.textLabel.cxFont = FontAsset.PingFangSC_Regular.size(12)
//      self.btnCancel.textLabel.cxFont = FontAsset.Icon.size(24.0)
      self.btnCancel.addTarget(self, action: #selector(onClickedCancelButton(_:)), for: .touchUpInside)
      
      self.addSubview(self.contentView)
      self.contentView.addSubview(self.contentText)
      self.contentView.addSubview(self.cq_loadingView)
      self.contentView.addSubview(self.btnCancel)
    }
  
  func loading(with message:String? = nil){
        if self.isLoading {
            return
        }
        self.isLoading = true
        self.cq_loadingView.startAnimating()
        if let tempMessage = message,tempMessage.count >= 1 {
          self.contentText.cxText = tempMessage
          let maxWidth:CGFloat = UIScreen.main.bounds.width * 0.75
          let maxSize = CGSize(width: maxWidth, height: CGFloat(MAXFLOAT))
          let font = self.contentText.cxFont
          let size = tempMessage.sizeWithConstrainedSize(size: maxSize, font: font, lineSpacing: 1.0)
          let validWidth:CGFloat = max(100.0, size.width + 30.0)
          self.contentView.snp.remakeConstraints { (make) in
            make.centerX.centerY.equalToSuperview()
            make.width.equalTo(validWidth)
          }
          self.cq_loadingView.snp.remakeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(45.0)
            make.top.equalToSuperview().offset(24.0)
          }
          if self.cancelAction != nil {
            self.contentText.snp.remakeConstraints { (make) in
              make.top.equalTo(self.cq_loadingView.snp.bottom).offset(16.0)
              make.leading.equalToSuperview().offset(15.0)
              make.trailing.equalToSuperview().offset(-15.0)
            }
            self.btnCancel.snp.remakeConstraints { (make) in
              make.top.equalTo(self.contentText.snp.bottom).offset(8.0)
              make.centerX.equalToSuperview()
              make.bottom.equalToSuperview().offset(-8.0)
              make.width.height.equalTo(40.0)
            }
          }else{
            self.contentText.snp.remakeConstraints { (make) in
              make.top.equalTo(self.cq_loadingView.snp.bottom).offset(8.0)
              make.leading.equalToSuperview().offset(15.0)
              make.trailing.equalToSuperview().offset(-15.0)
              make.bottom.equalToSuperview().offset(-12.0)
            }
          }
        }else{
          if self.cancelAction != nil {
            self.contentView.snp.remakeConstraints { (make) in
              make.centerX.centerY.equalToSuperview()
              make.width.equalTo(100.0)
            }
            self.cq_loadingView.snp.remakeConstraints { (make) in
              make.centerX.equalToSuperview()
              make.width.height.equalTo(45.0)
              make.top.equalToSuperview().offset(24.0)
            }
            self.btnCancel.snp.remakeConstraints { (make) in
              make.top.equalTo(self.cq_loadingView.snp.bottom).offset(16.0)
              make.centerX.equalToSuperview()
              make.bottom.equalToSuperview().offset(-16.0)
              make.width.height.equalTo(40.0)
            }
            
          }else{
            self.contentView.snp.remakeConstraints { (make) in
              make.centerX.centerY.equalToSuperview()
              make.width.equalTo(100.0)
            }
            self.cq_loadingView.snp.remakeConstraints { (make) in
              make.leading.top.trailing.bottom.equalToSuperview()
            }
          }
          self.contentText.snp.remakeConstraints { (make) in}
        }
    }
  
    func stopLoad(){
      self.isLoading = false
      self.cq_loadingView.stopAnimating()
    }
    @IBAction func onClickedCancelButton(_ sender:UIButton){
        self.cancelAction?()
    }
  
}
